/*
 * Copyright (C) 2021 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 *
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * Purpose : Definition of ME handler: XG-PON upstream management performance monitoring history data (346)
 *
 * Feature : The file includes the following modules and sub-modules
 *           (1) ME handler: XG-PON upstream management performance monitoring history data  (346)
 */

#include "app_basic.h"
#ifndef OMCI_X86
#include "rtk/stat.h"
#endif

MIB_TABLE_INFO_T gMibXgponUsMangPmTableInfo;
MIB_ATTR_INFO_T  gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ATTR_NUM];
MIB_TABLE_XGPONUSMANGPMHD_T gMibXgponUsMangPmDefRow;
MIB_TABLE_OPER_T gMibXgponUsMangPmOper;

static UINT8    aXgponUsMangPmTcaAlmNumber[MIB_TABLE_XGPONUSMANGPMHD_ATTR_NUM];
static UINT8    aXgponUsMangPmTcaAttrMap[MIB_TABLE_XGPONUSMANGPMHD_ATTR_NUM];


GOS_ERROR_CODE XGPON_us_mang_pm_CfgMe(void* pOldRow, void* pNewRow, MIB_OPERA_TYPE operationType, MIB_ATTRS_SET attrSet, UINT32 pri)
{
    GOS_ERROR_CODE                  ret;
    UINT32                          entrySize;
    MIB_TABLE_INDEX                 tableIndex;
    MIB_TABLE_ANIG_T                mibAniG;
    MIB_TABLE_XGPONUSMANGPMHD_T         mibCurrentBin;
    MIB_TABLE_XGPONUSMANGPMHD_T        *pMibXgponUsMangPmhd;


    pMibXgponUsMangPmhd = (MIB_TABLE_XGPONUSMANGPMHD_T *)pNewRow;

    // check if corresponding ANI-G exists
    mibAniG.EntityID = pMibXgponUsMangPmhd->EntityId;
    ret = MIB_Get(MIB_TABLE_ANIG_INDEX, &mibAniG, sizeof(MIB_TABLE_ANIG_T));
    if (GOS_OK != ret)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Instance not found in %s: %s, 0x%x",
            __FUNCTION__, MIB_GetTableName(MIB_TABLE_ANIG_INDEX), mibAniG.EntityID);

        return GOS_FAIL;
    }


    tableIndex = MIB_TABLE_XGPON_US_MANG_PMHD_INDEX;
    entrySize = MIB_GetTableEntrySize(tableIndex);

    switch (operationType)
    {
        case MIB_ADD:
            // sync control block data from history bin to current bin
            if (GOS_OK != MIB_SetPmCurrentBin(tableIndex, pMibXgponUsMangPmhd, entrySize))
            {
                OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set current bin error in %s: %s, 0x%x",
                    __FUNCTION__, MIB_GetTableName(tableIndex), pMibXgponUsMangPmhd->EntityId);
            }

            ret = mib_alarm_table_add(tableIndex, pNewRow);
            break;

        case MIB_DEL:
            ret = mib_alarm_table_del(tableIndex, pOldRow);
            break;

        case MIB_SET:
            mibCurrentBin.EntityId = pMibXgponUsMangPmhd->EntityId;
            if (GOS_OK != MIB_GetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
            {
                OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Current bin not found in %s: %s, 0x%x",
                    __FUNCTION__, MIB_GetTableName(tableIndex), mibCurrentBin.EntityId);

                return GOS_FAIL;
            }

            // sync control block data from history bin to current bin
            mibCurrentBin.ThresholdID = pMibXgponUsMangPmhd->ThresholdID;
            if (GOS_OK != MIB_SetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
            {
                OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set current bin error in %s: %s, 0x%x",
                    __FUNCTION__, MIB_GetTableName(tableIndex), mibCurrentBin.EntityId);
            }
            break;

        default:
            break;
    }

    return ret;
}

GOS_ERROR_CODE XGPON_us_mang_pm_handler(MIB_TABLE_INDEX        tableIndex,
                                                omci_me_instance_t      instanceID,
                                                omci_pm_oper_type_t     operType,
                                                BOOL                    *pIsTcaRaised)
{
    GOS_ERROR_CODE                  ret;
    UINT32                          entrySize;
    MIB_TABLE_ANIG_T                mibAniG;
    MIB_TABLE_XGPONUSMANGPMHD_T     mibXgponUsMangPm;
    MIB_TABLE_XGPONUSMANGPMHD_T     mibCurrentBin;
    omci_xgpon_stat_t               portCntrs;
    omci_xgpon_stat_t               *pPortCntrs = (omci_xgpon_stat_t*)&portCntrs;

    if (!pIsTcaRaised)
        return GOS_ERR_PARAM;

    // get table entry size
    entrySize = MIB_GetTableEntrySize(tableIndex);

    mibAniG.EntityID = instanceID;
    if (GOS_OK != MIB_Get(MIB_TABLE_ANIG_INDEX, &mibAniG, sizeof(MIB_TABLE_ANIG_T)))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Instance not found in %s: %s, 0x%x",
            __FUNCTION__, MIB_GetTableName(MIB_TABLE_ANIG_INDEX), mibAniG.EntityID);

        return GOS_FAIL;
    }

    mibXgponUsMangPm.EntityId = instanceID;
    if (GOS_OK != MIB_Get(tableIndex, &mibXgponUsMangPm, entrySize))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Instance not found in %s: %s, 0x%x",
            __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);

        return GOS_FAIL;
    }

    mibCurrentBin.EntityId = instanceID;
    if (GOS_OK != MIB_GetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Current bin not found in %s: %s, 0x%x",
            __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);

        return GOS_FAIL;
    }

    if (OMCI_PM_OPER_RESET == operType)
    {
        memset(&mibCurrentBin, 0, entrySize);
        mibCurrentBin.EntityId = instanceID;
        mibCurrentBin.ThresholdID = mibXgponUsMangPm.ThresholdID;

        if (GOS_OK != MIB_SetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set current bin error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }

        if (GOS_OK != MIB_Set(tableIndex, &mibCurrentBin, entrySize))
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set instance error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }

        return GOS_OK;
    }

    if(OMCI_PM_OPER_GET_CURRENT_DATA == operType)
    {
        omci_pm_update_xgpon_stat(OMCI_PM_OPER_GET_CURRENT_DATA, OMCI_XGPON_PM_BITMAP_TX_PLAOM);

    }

    if (OMCI_PM_OPER_UPDATE == operType ||
        OMCI_PM_OPER_UPDATE_AND_SWAP == operType ||
        OMCI_PM_OPER_GET_CURRENT_DATA == operType)

    {
        // update PM values
        if (GOS_OK == omci_pm_getCurrent_xgpon_stat(instanceID,pPortCntrs))
        {
            m_omci_pm_update_reset_u32_attr(pPortCntrs->usPloamMsgCnt, mibCurrentBin.UsPloamMsgCnt);
            m_omci_pm_update_reset_u32_attr(pPortCntrs->snOnuMsgCnt, mibCurrentBin.SnOnuMsgCnt);
            m_omci_pm_update_reset_u32_attr(pPortCntrs->registrationMsgCnt, mibCurrentBin.RegistrationMsgCnt);
            m_omci_pm_update_reset_u32_attr(pPortCntrs->keyReportMsgCnt, mibCurrentBin.KeyReportMsgCnt);
            m_omci_pm_update_reset_u32_attr(pPortCntrs->ackMsgCnt, mibCurrentBin.AckMsgCnt);
            m_omci_pm_update_reset_u32_attr(pPortCntrs->slpRequestMsgCnt, mibCurrentBin.SlpRequestMsgCnt);
        }

        if (GOS_OK != MIB_SetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set current bin error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }

        ret = omci_pm_is_threshold_crossed(tableIndex, instanceID,
                &mibCurrentBin, aXgponUsMangPmTcaAlmNumber, aXgponUsMangPmTcaAttrMap, NULL, pIsTcaRaised);
        if (GOS_OK != ret && GOS_ERR_NOT_FOUND != ret)
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Check threshold error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }
    }

    if (OMCI_PM_OPER_SWAP == operType || OMCI_PM_OPER_UPDATE_AND_SWAP == operType)
    {
        // swap PM values
        mibCurrentBin.IntEndTime++;
        mibXgponUsMangPm.IntEndTime = mibCurrentBin.IntEndTime;

        if (GOS_OK != MIB_Set(tableIndex, &mibCurrentBin, entrySize))
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set instance error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }

        memset(&mibCurrentBin, 0, entrySize);
        mibCurrentBin.EntityId = instanceID;
        mibCurrentBin.ThresholdID = mibXgponUsMangPm.ThresholdID;
        mibCurrentBin.IntEndTime = mibXgponUsMangPm.IntEndTime;

        if (GOS_OK != MIB_SetPmCurrentBin(tableIndex, &mibCurrentBin, entrySize))
        {
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Set current bin error in %s: %s, 0x%x",
                __FUNCTION__, MIB_GetTableName(tableIndex), instanceID);
        }
    }

    return GOS_OK;
}

GOS_ERROR_CODE mibTable_init(MIB_TABLE_INDEX tableId)
{
    if(!IS_G987_SYSTEM())
    {
        OMCI_PRINT("Ignore classId:%d,only support on G.987 ONU", OMCI_ME_CLASS_XGPON_US_MANG_PMHD);
        return GOS_FAIL;
    }

    gMibXgponUsMangPmTableInfo.Name = "XgponUsMangPm";
    gMibXgponUsMangPmTableInfo.ShortName = "XGPONUSMANGPMHD";
    gMibXgponUsMangPmTableInfo.Desc = " XG-PON uptream management performance monitoring history data ";
    gMibXgponUsMangPmTableInfo.ClassId = (UINT32)(OMCI_ME_CLASS_XGPON_US_MANG_PMHD);
    gMibXgponUsMangPmTableInfo.InitType = (UINT32)(OMCI_ME_INIT_TYPE_OLT);
    gMibXgponUsMangPmTableInfo.StdType = (UINT32)(OMCI_ME_TYPE_STANDARD_PM);
    gMibXgponUsMangPmTableInfo.ActionType = (UINT32)(OMCI_ME_ACTION_CREATE | OMCI_ME_ACTION_DELETE | OMCI_ME_ACTION_SET | OMCI_ME_ACTION_GET | OMCI_ME_ACTION_GET_CURRENT_DATA);
    gMibXgponUsMangPmTableInfo.pAttributes = &(gMibXgponUsMangPmAttrInfo[0]);

    gMibXgponUsMangPmTableInfo.attrNum = MIB_TABLE_XGPONUSMANGPMHD_ATTR_NUM;
    gMibXgponUsMangPmTableInfo.entrySize = sizeof(MIB_TABLE_XGPONUSMANGPMHD_T);
    gMibXgponUsMangPmTableInfo.pDefaultRow = &gMibXgponUsMangPmDefRow;

    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Name = "EntityId";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].Name = "IntEndTime";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].Name = "ThresholdID";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Name = "UsPloamMsgCnt";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].Name = "SnOnuMsgCnt";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Name = "RegistrationMsgCnt";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].Name = "KeyReportMsgCnt";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Name = "AckMsgCnt";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Name = "SlpRequestMsgCnt";


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Entity ID";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Interval End Time";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Threshold Data ID";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Upstream  PLOAM  message  count";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Serial_number_ONU message count";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Registration  message  count";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].Desc = "Key_report message count";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Acknowledge  message  count";
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Sleep_request  message  count";



    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT16;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT8;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT16;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT32;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Len = 2;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].Len = 1;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].Len = 2;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Len = 4;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].Len = 4;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Len = 4;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].Len = 4;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Len = 4;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].Len = 4;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = TRUE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = FALSE;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;



    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_SBC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE | OMCI_ME_ATTR_ACCESS_SBC;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;


    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_INTENDTIME_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_THRESHOLDID_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_USPLOAMMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SNONUMSG_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_REGISTRATIONMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_KEYREPORTMSGINDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_ACKMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);
    gMibXgponUsMangPmAttrInfo[MIB_TABLE_XGPONUSMANGPMHD_SLPREQUESTMSGCNT_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = (OMCI_ME_ATTR_TYPE_MANDATORY | OMCI_ME_ATTR_TYPE_PM);


    memset(&(gMibXgponUsMangPmDefRow.EntityId), 0x00, sizeof(gMibXgponUsMangPmDefRow.EntityId));
    memset(&(gMibXgponUsMangPmDefRow.IntEndTime), 0x00, sizeof(gMibXgponUsMangPmDefRow.IntEndTime));
    memset(&(gMibXgponUsMangPmDefRow.ThresholdID), 0x00, sizeof(gMibXgponUsMangPmDefRow.ThresholdID));
    memset(&(gMibXgponUsMangPmDefRow.UsPloamMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.UsPloamMsgCnt));
    memset(&(gMibXgponUsMangPmDefRow.SnOnuMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.SnOnuMsgCnt));
    memset(&(gMibXgponUsMangPmDefRow.RegistrationMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.RegistrationMsgCnt));
    memset(&(gMibXgponUsMangPmDefRow.KeyReportMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.KeyReportMsgCnt));
    memset(&(gMibXgponUsMangPmDefRow.AckMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.AckMsgCnt));
    memset(&(gMibXgponUsMangPmDefRow.SlpRequestMsgCnt), 0x00, sizeof(gMibXgponUsMangPmDefRow.SlpRequestMsgCnt));


    memset(&gMibXgponUsMangPmOper, 0x0, sizeof(MIB_TABLE_OPER_T));
	gMibXgponUsMangPmOper.meOperDrvCfg = XGPON_us_mang_pm_CfgMe;
    gMibXgponUsMangPmOper.meOperConnCfg = NULL;
	gMibXgponUsMangPmOper.meOperConnCheck = NULL;
	gMibXgponUsMangPmOper.meOperAvlTreeAdd = NULL;
    gMibXgponUsMangPmOper.meOperDump = omci_mib_oper_dump_default_handler;
    gMibXgponUsMangPmOper.meOperPmHandler = XGPON_us_mang_pm_handler;

    // for PM ME only, indicates the Threshold value attribute mapping and alarm number
    memset(aXgponUsMangPmTcaAlmNumber, 0, sizeof(aXgponUsMangPmTcaAlmNumber));

    memset(aXgponUsMangPmTcaAttrMap, 0, sizeof(aXgponUsMangPmTcaAttrMap));


    MIB_TABLE_XGPON_US_MANG_PMHD_INDEX = tableId;
    MIB_InfoRegister(tableId, &gMibXgponUsMangPmTableInfo, &gMibXgponUsMangPmOper);

    return GOS_OK;
}
